/*******************************************************************************
File: 2_main_outcomes
Project: CPKIN
Purpose: This .do file cleans and analyzes the main outcomes
Author: Akshay Dixit
Date Created: Jan 23, 2019
Date modified: June 28, 2020
********************************************************************************/

clear all
set more off

***THIS IS THE INPUT THAT MUST BE CHANGED SPECIFIC TO THE USER***
*gl identity ""
*gl user "" 

********************************************************************************

***Creating Global File Paths***'
gl data "$user/data"
gl dof "$user/do files"
gl analysis "$user/analysis"

********************************************************************************

u "$data/endline_adult_chief_child_data.dta", clear

* Stratification variable - Chiefdom

gl chiefdom = "bagruwa_chief barri_chief bumpeh_chief fakunya_chief gallinas_perri_chief kaiyamba_chief kamarjei_chief kori_chief kowa_chief kpaka_chief malin_chief sowa_chief"

* Control variables

gl controls = "wealth_index female age attend_school fp_visit vil_pop"

***Main outcome variables***

foreach var in have_fp_fp_role have_fp_oth_vilChd_knw_FP have_fp_vilAdults_kowFP_amount have_fp_peo_know_fp have_fp_chd_know_fp {
	replace `var' = "1" if `var' == "yes"
	replace `var' = "0" if `var' == "no"
	destring `var', replace
	tab `var'
}
	
	//Ever heard of the position of a child welfare focal person
tab treatment, sum(know_fp)

	//Ever spoken with the focal person in your village
replace speak_with_fp = have_fp_speak_with_sp if type_survey == "child"
g ever_spoken_with_fp = (speak_with_fp > 0)
replace ever_spoken_with_fp = . if speak_with_fp == .

	//Is focal person aware of his/her responsibilities
replace fp_role = have_fp_fp_role if type_survey == "child"
ren fp_role fp_aware

	//Do people in village know who the focal person is
g child_peo_know_fp = 1 if (have_fp_oth_vilChd_knw_FP == 1 | have_fp_vilAdults_kowFP_amount == 1)
replace child_peo_know_fp = 0 if (have_fp_oth_vilChd_knw_FP == 0 & have_fp_vilAdults_kowFP_amount == 0)
replace peo_know_fp = child_peo_know_fp if type_survey == "child"
ren peo_know_fp village_people_know_fp
	
	//Do members of village report incidents of child violations to your village focal person
replace rpt_vio_to_fp = have_fp_peo_know_fp if type_survey == "child"
ren rpt_vio_to_fp villagers_report_violations

* Replace missing values by 0 when know_fp == 0

local outcomes ever_spoken_with_fp fp_aware village_people_know_fp villagers_report_violations
foreach var of local outcomes {
	replace `var' = 0 if know_fp == 0
}

		/*
		The other 4 outcome variables are conditional upon the first outcome, "know_fp". 
		This can lead to a biased estimate of impact.
		Hence, the estimand is redefined by imputing 0 when know_fp = 0.
		*/	
		
* Label variables

lab var know_fp	"Ever heard of the position of a CPFP" 
lab var ever_spoken_with_fp "Ever spoken with CPFP"
lab var fp_aware "CPFP aware of their responsibilities"
lab var village_people_know_fp "Do others in village know CPFP"
lab var villagers_report_violations "Do villagers report to CPFP"

lab var treatment "Treatment"
lab var wealth_index "Wealth index" 
lab var female "Female"
lab var age "Age"
lab var attend_school "Ever attended school" 
lab var vil_pop "Village population"
lab var fp_visit "Village visited by UNICEF monitoring team"
lab var chief "Respondent: Chief"
lab var sms_received "SMS received"


	//Merge with baseline
drop _merge
merge m:1 village_id1 using "$data/baseline_social_capital.dta"	
		
		/*
		1 village (in Pujehun) doesn't match. Data on this village is missing from baseline.
		There are 7 observations from this village - 6 adult, 1 chief.
		*/
		
********************************************************************************

***ITT analysis***

cd "$analysis"

cap erase "ITT_main_outcomes.xls"

local outcomes know_fp ever_spoken_with_fp fp_aware village_people_know_fp villagers_report_violations
foreach var of local outcomes {
	
	di "`var'"
	tab treatment, sum(`var')
	
	qui sum `var' if treatment == 0
	local control_mean = (r(mean))
	local rounded = round(`control_mean', 0.01)
	
	qui reg `var' treatment $chiefdom $controls chief, vce(cluster village_id1)
	
	local p = 2 * (1-normal(abs(_b[treatment]/_se[treatment])))
	local p_round = round(`p', 0.001)
	
	outreg2 using "ITT_main_outcomes.xls", append label keep(treatment wealth_index female age attend_school fp_visit vil_pop chief) ///
	addtext(Naive p-value, "`p_round'", Control group mean, "`rounded'") ///
	addnote("Standard errors clustered at the village-level. Specifications include binary variables for randomization strata.")
	
}

cap erase "ITT_main_outcomes.txt" 

********************************************************************************

***TOT analysis with sms_received as instrument***

cap erase "TOT_main_outcomes.xls"
	
local outcomes know_fp ever_spoken_with_fp fp_aware village_people_know_fp villagers_report_violations 
foreach var of local outcomes {
	
	di "`var'"
	
	qui sum `var' if treatment == 0
	local control_mean = (r(mean))
	local rounded = round(`control_mean', 0.01)
	
	qui ivregress 2sls `var' $chiefdom $controls chief (sms_received = treatment), vce(cluster village_id1)
	
	local p = 2 * (1-normal(abs(_b[sms_received]/_se[sms_received])))
	local p_round = round(`p', 0.001)
	
	outreg2 using "TOT_main_outcomes.xls", append label keep(sms_received wealth_index female age attend_school fp_visit vil_pop chief) ///
	addtext(Naive p-value, "`p_round'", Control group mean, "`rounded'") ///
	addnote("Standard errors clustered at the village-level. Specifications include binary variables for randomization strata.") 
	
}

cap erase "TOT_main_outcomes.txt"

********************************************************************************

***Sub-group analysis***

* Generate variables for subgroup analysis

	//Whether village was visited by UNICEF team
g treatment_fp_visit = treatment*fp_visit

	//Baseline social capital
sum bl_social_capital, d
g low_capital = (bl_social_capital < (r(p50)))
replace low_capital = . if bl_social_capital == .
g treatment_low_capital = treatment*low_capital

* Label variables

lab var low_capital "Low social capital (baseline)"
lab var treatment_fp_visit "Treatment * UNICEF monitoring team"
lab var treatment_low_capital "Treatment * Low social capital"

* Analysis

cap erase "subgroup_unicef.xls"
cap erase "subgroup_social_capital.xls"

local outcomes know_fp ever_spoken_with_fp fp_aware village_people_know_fp villagers_report_violations
foreach var of local outcomes {
	
	di "`var'"
	tab treatment, sum(`var')
	
	qui sum `var' if treatment == 0
	local control_mean = (r(mean))
	local rounded = round(`control_mean', 0.01)
	
		//By UNICEF monitoring
	qui reg `var' treatment treatment_fp_visit $chiefdom $controls chief, vce(cluster village_id1)
	
	outreg2 using "subgroup_unicef.xls", append label keep(treatment treatment_fp_visit fp_visit) ///
	addnote("Note: Standard errors clustered at the village-level. Specifications include binary variables for randomization strata and controls as in Equation (1)")
	
		//By social capital
	qui reg `var' treatment treatment_low_capital low_capital $chiefdom $controls chief, vce(cluster village_id1)
	
	outreg2 using "subgroup_social_capital.xls", append label keep(treatment treatment_low_capital low_capital) ///
	addnote("Note: Standard errors clustered at the village-level. Specifications include binary variables for randomization strata and controls as in Equation (1)")
	
}

cap erase "subgroup_unicef.txt"
cap erase "subgroup_social_capital.txt"

********************************************************************************

clear


